package com.lebang.controller.sys;

import com.alibaba.fastjson.JSONObject;
import com.lebang.common.Login;
import com.lebang.configuration.datasource.DataSource;
import com.lebang.domain.PkgEarnInfo;
import com.lebang.example.PkgEarnInfoExample;
import com.lebang.service.sys.PkgEarnInfoService;
import com.lebangweb.common.TextUtil;
import com.lebangweb.response.Callback;
import com.lebangweb.response.JsonData;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;


/**
 * pkg_earn_info
 *
 * @author hwq
 * @date 2020-06-17
 */
@Controller
@RequestMapping("/sys/pkgearninfo")
public class PkgEarnInfoController {
    @Autowired
    private PkgEarnInfoService baseService;
    @Login
    @GetMapping("/list")
    public String list() {
        return "/pkgearninfo/list";
    }
    @Login
    @DataSource("commonDB")
    @GetMapping("/add")
    public String add(Integer id, Model model) {
        PkgEarnInfo vo;
        if (id != null) {
            vo = baseService.selectById(id);
        } else {
            vo = new PkgEarnInfo();
        }
        model.addAttribute("item", vo);
        return "/pkgearninfo/edit";
    }
    @Login
    @DataSource("commonDB")
    @PostMapping("/edit")
    @ResponseBody
    public JsonData edit(PkgEarnInfo vo) {
        if (vo.getId() != null) {
            vo.setModifyTime(LocalDateTime.now());
            baseService.updateById(vo);
        } else {
            vo.setCreateTime(LocalDateTime.now());
            vo.setHasDeleted(0);
            baseService.insertModel(vo);
        }
        return Callback.success("success", "操作成功");
    }
    @DataSource("commonDB")
    @Login
    @PostMapping("/delete")
    @ResponseBody
    public JsonData delete(Integer id) {
        baseService.deleteById(id);
        return Callback.success("success", "操作成功");
    }
    @Login
    @DataSource("commonDB")
    @ApiOperation(value = "分页")
    @PostMapping("/list")
    @ResponseBody
    public JSONObject list(HttpServletRequest request, @RequestParam Integer page, @RequestParam Integer limit
    , String pkgName, String appName,  @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime star,
                           @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime end
    ,String field,String order) {
        PkgEarnInfoExample example = new PkgEarnInfoExample();
        example.setOffset((page - 1) * limit.longValue());
        example.setLimit(limit);
        if(!TextUtil.isEmpty(field,order)){
            example.setOrderByClause(field+"  "+order);
        }
        PkgEarnInfoExample.Criteria criteria = example.createCriteria();
        if(!TextUtil.isEmpty(pkgName)){
            criteria.andPkgNameLike(pkgName+"%");
        }
        if(!TextUtil.isEmpty(appName)){
            criteria.andAppNameLike(appName+"%");
        }
        if(!TextUtil.isEmpty(star,end)){
            criteria.andCreateTimeBetween(star,end);
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", 0);
        jsonObject.put("data", baseService.selectByExample(example));
        jsonObject.put("count", baseService.countByExample(example));
        return jsonObject;
    }
    @DataSource("commonDB")
    @Login
    @PostMapping("/enable")
    @ResponseBody
    public JsonData enable(PkgEarnInfo vo) {
        baseService.updateById(vo);
        return Callback.success("success", "操作成功");
    }
    @DataSource("commonDB")
    @Login
    @PostMapping("/enable/click")
    @ResponseBody
    public JsonData click(PkgEarnInfo vo) {
        baseService.updateById(vo);
        return Callback.success("success", "操作成功");
    }
    @Login
    @DataSource("commonDB")
    @PostMapping("/enable/click/list")
    @ResponseBody
    public JsonData black(HttpServletRequest request, String str,Integer type) {
        List<String> stringList = Arrays.asList(str.split(","));
        baseService.enableList(stringList,type);
        return Callback.success("success", "操作成功");
    }
}
